package me.shurufa.activities;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import cn.finalteam.okhttpfinal.HttpRequest;
import me.shurufa.R;
import me.shurufa.net.API;
import me.shurufa.net.BaseResponse;
import me.shurufa.net.RequestParams;
import me.shurufa.net.RequestServerTask;
import me.shurufa.net.okhttp.callback.HttpCallback;
import me.shurufa.response.base.BaseResp;
import me.shurufa.utils.Global;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.Utils;

/**
 * 从SettingActivity点击反馈跳转页面
 *
 * @FileName: me.shurufa.activities.FeedBackActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-19 11:45
 */
public class FeedBackActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.right_text)
    TextView rightText;
    @Bind(R.id.feedback_edit)
    EditText feedbackEdit;

    private InputMethodManager imm;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_feedback;
        super.onCreate(savedInstanceState);
        initUI();
    }

    private void initUI() {
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        titleTextView.setText(getString(R.string.feedback));
        rightText.setVisibility(View.VISIBLE);
        rightText.setText(getString(R.string.send));
        rightText.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_text:
                imm.hideSoftInputFromWindow(feedbackEdit.getWindowToken(), 0);
                final String text = feedbackEdit.getText().toString().trim();
                if (!TextUtils.isEmpty(text) && text.length() > 6) {

                    reqSubmitSuggest(text);

                } else if (TextUtils.isEmpty(text)) {
                    Utils.showToast(R.string.content_is_empty);
                } else {
                    Utils.showToast(R.string.content_must_greater_than_6);
                }
                break;
        }
    }

    /**
     * 提交建议
     * @param text
     */
    private void reqSubmitSuggest(String text) {
        RequestParams params = new RequestParams();
        params.addFormDataPart("content", text);
        HttpRequest.post(API.SUBMIT_SUGGEST, params, new HttpCallback<BaseResp>() {
            @Override
            public void onRespSuccess(BaseResp resp) {
                try {
                    Utils.showToast(getString(R.string.commit_success));
                    feedbackEdit.setText("");
                } catch (Exception e) {
                }
            }

            @Override
            public void onRespFailure(BaseResp resp) {

            }

        });
    }
}
